package com.wwwarehouse.usercenter.fragment.register;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.wwwarehouse.common.custom_widget.AdderSubtractorView;
import com.wwwarehouse.common.custom_widget.keyboard.VirtualKeyboardView;
import com.wwwarehouse.common.custom_widget.textinput.CountTextLayout;
import com.wwwarehouse.common.custom_widget.textinput.TextInputLayout;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.common.UserCenterCommon;

import java.lang.reflect.Method;
import java.math.BigDecimal;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/8/15 9:21
 * @描述 ${TODO}
 */
public class EnterEmployeeDataFragment extends RegisterCardBaseFragment implements View.OnClickListener, AdderSubtractorView.DNumberListener, View.OnFocusChangeListener, TextWatcher {

    private View                mRootView;
    private AdderSubtractorView mAsvHeight;
    private AdderSubtractorView mAsvWeight;
    private ImageView           mCbMyopia;
    private ImageView           mCbColorBlind;
    private ImageView           mCbColorWeek;
    private ImageView           mCbPresbyopia;
    private ImageView           mIvYes;
    private ImageView           mIvNo;
    private EditText            mEtDisSit;
    private Button              mBtSubmit;
    private LinearLayout        mLlYse;
    private LinearLayout        mLlNo;
    private LinearLayout        mLlMyopia;
    private LinearLayout        mLlColorBlind;
    private LinearLayout        mLlColorWeek;
    private LinearLayout        mLlPresbyopia;
    private String mEyesight = "";
    private RelativeLayout      mRlElse;
    private CountTextLayout     mCountTextLayout;
    private VirtualKeyboardView mHwKeyboard;
    private TextInputLayout     mHeightLayout;
    private TextInputLayout     mWeightLayout;
    private EditText            mEtHeight;
    private EditText            mEtWeight;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_entry_employee_data, container, false);
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mHwKeyboard = findView(mRootView, R.id.virtualKeyboardView);
        mHwKeyboard.setOnConfirmListener(new VirtualKeyboardView.OnConfirmListener() {
            @Override
            public void confirm() {
                mHwKeyboard.setVisibility(View.INVISIBLE);
            }
        });
        mCountTextLayout = findView(mRootView, R.id.ctl);
        mRlElse = findView(mRootView, R.id.rl_else);
        mRlElse.setOnClickListener(this);

        //身高
        mAsvHeight = findView(mRootView, R.id.height);
        mAsvHeight.setStyle(2);
        mAsvHeight.setLeastValue(130);
        mAsvHeight.setMaximumValue(240);
        mAsvHeight.setDefaultValue(170);
        mAsvHeight.setDoubleNumberListener(this);
        mEtHeight = mAsvHeight.getEditText();
        mEtHeight.setOnClickListener(this);
        mEtHeight.setOnFocusChangeListener(this);
        mEtHeight.addTextChangedListener(this);

        //体重
        mAsvWeight = findView(mRootView, R.id.weights);
        mAsvWeight.setStyle(2);
        mAsvWeight.setLeastValue(30);
        mAsvWeight.setMaximumValue(260);
        mAsvWeight.setDefaultValue(60);
        mAsvWeight.setDoubleNumberListener(this);
        mEtWeight = mAsvWeight.getEditText();
        mEtWeight.setOnClickListener(this);
        mEtWeight.setOnFocusChangeListener(this);
        mEtWeight.addTextChangedListener(this);

        hideSystemKeyboard(mEtHeight, mEtWeight);
        //近视
        mCbMyopia = findView(mRootView, R.id.cb_myopia);
        mLlMyopia = findView(mRootView, R.id.ll_myopia);
        mLlMyopia.setOnClickListener(this);
        //色盲
        mCbColorBlind = findView(mRootView, R.id.cb_color_blind);
        mLlColorBlind = findView(mRootView, R.id.ll_color_blind);
        mLlColorBlind.setOnClickListener(this);
        //色弱
        mCbColorWeek = findView(mRootView, R.id.cb_color_week);
        mLlColorWeek = findView(mRootView, R.id.ll_color_week);
        mLlColorWeek.setOnClickListener(this);
        //老花
        mCbPresbyopia = findView(mRootView, R.id.cb_presbyopia);
        mLlPresbyopia = findView(mRootView, R.id.ll_presbyopia);
        mLlPresbyopia.setOnClickListener(this);
        //是否残疾 yes
        mIvYes = findView(mRootView, R.id.iv_yes);
        mLlYse = findView(mRootView, R.id.ll_yes);
        mLlYse.setOnClickListener(this);
        //是否残疾 no
        mIvNo = findView(mRootView, R.id.iv_no);
        mLlNo = findView(mRootView, R.id.ll_no);
        mLlNo.setOnClickListener(this);
        //描述情况
        mEtDisSit = findView(mRootView, R.id.et_dis_sit);
        //        mEtDisSit.setEnabled(false);
        //提交
        mBtSubmit = findView(mRootView, R.id.bt_submit);
        mBtSubmit.setOnClickListener(this);
        if (UserCenterCommon.getInstance().isSubmit()) {
            isYes = UserCenterCommon.getInstance().getIsWeek();
            isNo = !isYes;
            if (isYes) {
                mCountTextLayout.setVisibility(View.VISIBLE);
                mIvYes.setBackground(getResources().getDrawable(R.drawable.feicanji));
                mIvNo.setBackground(getResources().getDrawable(R.drawable.canji));
            } else {
                mCountTextLayout.setVisibility(View.INVISIBLE);
                mIvYes.setBackground(getResources().getDrawable(R.drawable.canji));
                mIvNo.setBackground(getResources().getDrawable(R.drawable.feicanji));
            }
            if (UserCenterCommon.getInstance().getHeight() != null && UserCenterCommon.getInstance().getWeight() != null) {
                mEtHeight.setText(UserCenterCommon.getInstance().getHeight().toString());
                mEtWeight.setText(UserCenterCommon.getInstance().getWeight().toString());
            }
            if (UserCenterCommon.getInstance().getEyesight() != null) {
                if (UserCenterCommon.getInstance().getEyesight().contains("1")) {
                    isMyopia = true;
                    mCbMyopia.setBackground(getResources().getDrawable(R.drawable.checkbox));
                }
                if (UserCenterCommon.getInstance().getEyesight().contains("2")) {
                    isColorBlind = true;
                    mCbColorBlind.setBackground(getResources().getDrawable(R.drawable.checkbox));
                }
                if (UserCenterCommon.getInstance().getEyesight().contains("3")) {
                    isColorWeek = true;
                    mCbColorWeek.setBackground(getResources().getDrawable(R.drawable.checkbox));
                }
                if (UserCenterCommon.getInstance().getEyesight().contains("4")) {
                    isPresbyopia = true;
                    mCbPresbyopia.setBackground(getResources().getDrawable(R.drawable.checkbox));
                }
            }
            if (UserCenterCommon.getInstance().getDisabilitySituation() != null) {
                mEtDisSit.setText(UserCenterCommon.getInstance().getDisabilitySituation());
            }
        }


    }

    public void showKeyboardDouble(EditText editText) {
        mHwKeyboard.setFocusable(true);
        mHwKeyboard.setFocusableInTouchMode(true);
        mHwKeyboard.setEditText(editText);
        if (mHwKeyboard.getVisibility() != View.VISIBLE) {
            mHwKeyboard.setVisibility(View.VISIBLE);
        }
    }
    private void hideSystemKeyboard(EditText... mNumber) {
        for (int i = 0; i < mNumber.length; i++) {
            // 设置不调用系统键盘
            if (android.os.Build.VERSION.SDK_INT <= 10) {
                mNumber[i].setInputType(InputType.TYPE_NULL);
            } else {
                mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

                Class<EditText> clazz = EditText.class;
                Method method;

                try {
                    method = clazz.getMethod("setShowSoftInputOnFocus", boolean.class);
                    method.setAccessible(true);
                    method.invoke(mNumber[i], false);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    method = clazz.getMethod("setSoftInputShownOnFocus", boolean.class);
                    method.setAccessible(true);
                    method.invoke(mNumber[i], false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof EnterEmployeeDataFragment) {
            mActivity.setTitle(R.string.base_message);
        }
    }

    @Override
    public void requestDatas() {

    }

    private boolean isYes;
    private boolean isNo = true;
    private boolean isMyopia;//近视
    private boolean isColorBlind;//色盲
    private boolean isColorWeek;//色弱
    private boolean isPresbyopia;//老花

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (!TextUtils.isEmpty(mEtHeight.getText().toString().trim())) {
            if (Integer.parseInt(mEtHeight.getText().toString()) < 130) {
                mEtHeight.setText("130");
            }
        }else {
            mEtHeight.setText("170");
        }
        if (!TextUtils.isEmpty(mEtWeight.getText().toString().trim())) {
            if (Integer.parseInt(mEtWeight.getText().toString()) < 30) {
                mEtWeight.setText("30");
            }
        }else {
            mEtWeight.setText("60");
        }

        if (v != mEtHeight && v != mEtWeight) {
            if (mHwKeyboard.getVisibility() == View.VISIBLE) {
                mHwKeyboard.setVisibility(View.INVISIBLE);
            }

        }

        if (v == mEtWeight) {
            showKeyboardDouble(mEtWeight);
//            mHwKeyboard.setVisibility(View.VISIBLE);
        }
        if (v == mEtHeight) {
            showKeyboardDouble(mEtHeight);
//            mHwKeyboard.setVisibility(View.VISIBLE);
        }

        if (i != R.id.et_dis_sit) {
            hideSoftKeyBoard(v);
        }
        if (i == R.id.ll_yes) {
            if (!isYes) {
                mCountTextLayout.setVisibility(View.VISIBLE);
                mIvYes.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_true));
                mIvNo.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_false));
                isYes = true;
                isNo = false;
            }

        } else if (i == R.id.ll_no) {
            if (!isNo) {
                mCountTextLayout.setVisibility(View.INVISIBLE);
                mIvYes.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_false));
                mIvNo.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_true));
                isNo = true;
                isYes = false;
            }

        } else if (i == R.id.ll_myopia) {
            isMyopia = !isMyopia;
            if (isMyopia) {
                mCbMyopia.setBackground(getResources().getDrawable(R.drawable.checkbox));
            } else {
                mCbMyopia.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_false));
            }

        } else if (i == R.id.ll_color_blind) {
            isColorBlind = !isColorBlind;
            if (isColorBlind) {
                mCbColorBlind.setBackground(getResources().getDrawable(R.drawable.checkbox));
            } else {
                mCbColorBlind.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_false));
            }

        } else if (i == R.id.ll_color_week) {
            isColorWeek = !isColorWeek;
            if (isColorWeek) {
                mCbColorWeek.setBackground(getResources().getDrawable(R.drawable.checkbox));
            } else {
                mCbColorWeek.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_false));
            }

        } else if (i == R.id.ll_presbyopia) {
            isPresbyopia = !isPresbyopia;
            if (isPresbyopia) {
                mCbPresbyopia.setBackground(getResources().getDrawable(R.drawable.checkbox));
            } else {
                mCbPresbyopia.setBackground(getResources().getDrawable(R.drawable.radiobutton_checked_false));
            }
        } else if (i == R.id.bt_submit) {
            UserCenterCommon.getInstance().setHeight(new BigDecimal(mEtHeight.getText().toString().trim()));
            UserCenterCommon.getInstance().setWeight(new BigDecimal(mEtWeight.getText().toString().trim()));
            if (isMyopia) {
                mEyesight += "1";
            }
            if (isColorBlind) {
                mEyesight += "2";
            }
            if (isColorWeek) {
                mEyesight += "3";
            }
            if (isPresbyopia) {
                mEyesight += "4";
            }
            UserCenterCommon.getInstance().setEyesight(mEyesight);
            UserCenterCommon.getInstance().setWeek(isYes);
            if (isNo) {
               mEtDisSit.setText("");
            }
            UserCenterCommon.getInstance().setDisabilitySituation(mEtDisSit.getText().toString().trim());
            UserCenterCommon.getInstance().setSubmit(true);
            EventBus.getDefault().post("EnterEmployeeDataFragment");
            popFragment();

        } else if (i == R.id.rl_else) {
            hideSoftKeyBoard(v);
            if (mHwKeyboard.getVisibility() == View.VISIBLE) {
                mHwKeyboard.setVisibility(View.INVISIBLE);
            }

        }

    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (!TextUtils.isEmpty(mEtHeight.getText().toString().trim())) {
            if (Integer.parseInt(mEtHeight.getText().toString()) < 130) {
                mEtHeight.setText("130");
            }
        } else {
            mEtHeight.setText("170");
        }
        if (!TextUtils.isEmpty(mEtWeight.getText().toString().trim())) {
            if (Integer.parseInt(mEtWeight.getText().toString()) < 30) {
                mEtWeight.setText("30");
            }
        } else {
            mEtWeight.setText("60");
        }
        int i = view.getId();
        if (view == mEtWeight) {
            if (b) {
                showKeyboardDouble(mEtWeight);
                hideSoftKeyBoard(mEtDisSit);
            }
        }else if (view == mEtHeight) {
            if (b) {
                showKeyboardDouble(mEtHeight);
                hideSoftKeyBoard(mEtDisSit);
            }
        }else if (i == R.id.et_dis_sit) {
            if (!b) {
                hideSoftKeyBoard(mEtDisSit);
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHwKeyboard.getVisibility() == View.VISIBLE) {
            mHwKeyboard.setVisibility(View.INVISIBLE);
        }
        hideSoftKeyBoard(mEtDisSit);
    }

    @Override
    public void setMoreListener(double v, View view) {
    }

    @Override
    public void setLeastListener(double v, View view) {
        if (view == mAsvHeight) {
            mEtHeight.setText("130");
        } else if (view == mEtWeight) {
            mEtWeight.setText("30");
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
//        if (Integer.parseInt(mEtHeight.getText().toString()) < 130) {
//            mEtHeight.setText("130");
//        }
//        if (Integer.parseInt(mEtWeight.getText().toString()) < 30) {
//            mEtWeight.setText("30");
//        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
